package com.relytosoft.mizar.analyze.face.services.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.face.api.pojo.dto.AyFaceTaskDto;
import com.relytosoft.mizar.analyze.face.api.pojo.dto.AyFaceTaskUpdateDto;
import com.relytosoft.mizar.analyze.face.api.pojo.dto.AyTaskIdDto;
import com.relytosoft.mizar.analyze.face.api.pojo.query.AyFaceTaskQuery;
import com.relytosoft.mizar.analyze.face.api.pojo.query.AyFaceTaskSearchQuery;
import com.relytosoft.mizar.analyze.face.api.pojo.vo.AyBatchVo;
import com.relytosoft.mizar.analyze.face.api.pojo.vo.AyFaceTaskVo;
import com.relytosoft.mizar.analyze.face.services.service.AyFaceTaskService;
import com.relytosoft.mizar.analyze.face.services.service.AyFaceTaskSonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: zyc
 * @version: v1.0
 * @description: com.relytosoft.mizar.analyze.face.services.controller
 * @Date: 2022/5/13 10:01
 */
@Api(tags = {"人脸分析任务"})
@RestController
@RequestMapping("/face/task")
public class AyFaceTaskController {
    @Resource
    private AyFaceTaskService ayFaceTaskService;

    @Resource
    private AyFaceTaskSonService ayFaceTaskSonService;


    @ApiOperation("查询人脸分析任务列表")
    @GetMapping({"/list"})
    public ResponseResult<Map<String, Object>> select(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        AyFaceTaskQuery ayFaceTaskQuery = JSON.parseObject(bodys, AyFaceTaskQuery.class);
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        IPage<AyFaceTaskVo> page = ayFaceTaskService.selectAyFaceTaskPageList(pageQuery, ayFaceTaskQuery);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }


    @ApiOperation("修改人脸分析任务")
    @PutMapping({"/update"})
    public ResponseResult<Void> update(@RequestBody AyFaceTaskUpdateDto ayFaceTaskUpdateDto) {
        boolean result = ayFaceTaskService.updateAyFaceTask(ayFaceTaskUpdateDto);
        if (result) {
            return ResponseResult.ok("修改人脸任务成功");
        }
        return ResponseResult.fail("修改人脸任务失败");
    }


    @ApiOperation("创建人脸分析任务")
    @PostMapping({"/insert"})
    public ResponseResult<AyBatchVo> insert(@RequestBody AyFaceTaskDto ayFaceTaskDto) {
        AyBatchVo ayBatchVo = ayFaceTaskService.insertTaskByDto(ayFaceTaskDto);
        return ResponseResult.ok(ayBatchVo);
    }


    @ApiOperation("删除人脸分析任务")
    @PutMapping({"/delete"})
    public ResponseResult<Void> delete(@RequestBody AyTaskIdDto ayTaskIdDto) {
        if (ayFaceTaskService.deleteTaskByIds(ayTaskIdDto)) {
            ResponseResult.ok("已删除符合条件的任务");
        }
        return ResponseResult.fail("没有符合删除条件的任务");
    }


    @ApiOperation("启动人脸分析任务")
    @PostMapping({"/start"})
    public ResponseResult<Void> start(@RequestBody AyTaskIdDto ayTaskIdDto) {
        if (ayFaceTaskService.startTaskByIds(ayTaskIdDto)) {
            return ResponseResult.ok("已启动符合条件的任务");
        }
        return ResponseResult.fail("没有符合启动条件的任务");
    }


    @ApiOperation("停止人脸分析任务")
    @PostMapping({"/stop"})
    public ResponseResult<Void> stop(@RequestBody AyTaskIdDto ayTaskIdDto) {
        if (ayFaceTaskService.stopTaskByIds(ayTaskIdDto)) {
            return ResponseResult.ok("已停止符合条件的任务");
        }
        return ResponseResult.fail("没有符合停止条件的任务");
    }


    @ApiOperation("人脸任务结果检索")
    @GetMapping("/search")
    public ResponseResult<JSONObject> faceSearch(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyFaceTaskSearchQuery ayFaceTaskSearchQuery = JSONObject.parseObject(bodys, AyFaceTaskSearchQuery.class);
        return ResponseResult.ok(ayFaceTaskService.taskResultSearch(ayFaceTaskSearchQuery, pageQuery));
    }
}
